<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="服务商id" prop="agentId">
        <el-input
          v-model="queryParams.agentId"
          placeholder="请输入服务商id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现账单id" prop="billId">
        <el-input
          v-model="queryParams.billId"
          placeholder="请输入提现账单id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现金额" prop="withdrawAmount">
        <el-input
          v-model="queryParams.withdrawAmount"
          placeholder="请输入提现金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打款金额" prop="arriveAmount">
        <el-input
          v-model="queryParams.arriveAmount"
          placeholder="请输入打款金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手续费" prop="transectionFee">
        <el-input
          v-model="queryParams.transectionFee"
          placeholder="请输入手续费"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现税点" prop="withdrawRate">
        <el-input
          v-model="queryParams.withdrawRate"
          placeholder="请输入提现税点"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单笔提现费用" prop="withdrawFee">
        <el-input
          v-model="queryParams.withdrawFee"
          placeholder="请输入单笔提现费用"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打款时间" prop="arriveTime">
        <el-date-picker clearable
          v-model="queryParams.arriveTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择打款时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="代付订单号" prop="anotherPayBill">
        <el-input
          v-model="queryParams.anotherPayBill"
          placeholder="请输入代付订单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人" prop="auditBy">
        <el-input
          v-model="queryParams.auditBy"
          placeholder="请输入审核人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间" prop="auditTime">
        <el-date-picker clearable
          v-model="queryParams.auditTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['agent:withdraw:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['agent:withdraw:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['agent:withdraw:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['agent:withdraw:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="withdrawList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="服务商id" align="center" prop="agentId" v-if="columns[0].visible"/>
        <el-table-column label="提现账单id" align="center" prop="billId" v-if="columns[1].visible"/>
        <el-table-column label="提现金额" align="center" prop="withdrawAmount" v-if="columns[2].visible"/>
        <el-table-column label="打款金额" align="center" prop="arriveAmount" v-if="columns[3].visible"/>
        <el-table-column label="手续费" align="center" prop="transectionFee" v-if="columns[4].visible"/>
        <el-table-column label="提现税点" align="center" prop="withdrawRate" v-if="columns[5].visible"/>
        <el-table-column label="单笔提现费用" align="center" prop="withdrawFee" v-if="columns[6].visible"/>
        <el-table-column label="审核状态" align="center" prop="auditStatus" v-if="columns[7].visible"/>
        <el-table-column label="审核类型" align="center" prop="auditType" v-if="columns[8].visible"/>
        <el-table-column label="打款状态" align="center" prop="arriveStatus" v-if="columns[9].visible"/>
        <el-table-column label="打款时间" align="center" prop="arriveTime" width="180" v-if="columns[10].visible">
            <template #default="scope">
            <span>{{ parseTime(scope.row.arriveTime, '{y}-{m}-{d}') }}</span>
            </template>
        </el-table-column>
        <el-table-column label="代付订单号" align="center" prop="anotherPayBill" v-if="columns[11].visible"/>
        <el-table-column label="审核人" align="center" prop="auditBy" v-if="columns[12].visible"/>
        <el-table-column label="审核时间" align="center" prop="auditTime" width="180" v-if="columns[13].visible">
            <template #default="scope">
            <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
            </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" v-if="columns[14].visible"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['agent:withdraw:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['agent:withdraw:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务商体现审核对话框 -->
    <el-dialog :title="title" v-model="open" :width="dialogWidth" append-to-body>
      <el-form ref="withdrawRef" :model="form" :rules="rules">
      <el-row :gutter="20">

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="服务商id" prop="agentId">
          <el-input v-model="form.agentId" placeholder="请输入服务商id" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="提现账单id" prop="billId">
          <el-input v-model="form.billId" placeholder="请输入提现账单id" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="提现金额" prop="withdrawAmount">
          <el-input v-model="form.withdrawAmount" placeholder="请输入提现金额" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="打款金额" prop="arriveAmount">
          <el-input v-model="form.arriveAmount" placeholder="请输入打款金额" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="手续费" prop="transectionFee">
          <el-input v-model="form.transectionFee" placeholder="请输入手续费" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="提现税点" prop="withdrawRate">
          <el-input v-model="form.withdrawRate" placeholder="请输入提现税点" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="单笔提现费用" prop="withdrawFee">
          <el-input v-model="form.withdrawFee" placeholder="请输入单笔提现费用" />
        </el-form-item>
</el-col>




<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="打款时间" prop="arriveTime">
          <el-date-picker clearable
            v-model="form.arriveTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择打款时间">
          </el-date-picker>
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="代付订单号" prop="anotherPayBill">
          <el-input v-model="form.anotherPayBill" placeholder="请输入代付订单号" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="审核人" prop="auditBy">
          <el-input v-model="form.auditBy" placeholder="请输入审核人" />
        </el-form-item>
</el-col>

<el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="审核时间" prop="auditTime">
          <el-date-picker clearable
            v-model="form.auditTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
</el-col>

  <el-col :span="isTwoColumnForm ? 12 : 24">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        </el-col>
  </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Withdraw">
import { listWithdraw, getWithdraw, delWithdraw, addWithdraw, updateWithdraw } from "@/api/agent/withdraw"

const { proxy } = getCurrentInstance()

const withdrawList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

// 列显隐信息
const columns = ref([
{ key: 0, label: `服务商id`, visible: true },
{ key: 1, label: `提现账单id`, visible: true },
{ key: 2, label: `提现金额`, visible: true },
{ key: 3, label: `打款金额`, visible: true },
{ key: 4, label: `手续费`, visible: true },
{ key: 5, label: `提现税点`, visible: true },
{ key: 6, label: `单笔提现费用`, visible: true },
{ key: 7, label: `审核状态`, visible: true },
{ key: 8, label: `审核类型`, visible: true },
{ key: 9, label: `打款状态`, visible: true },
{ key: 10, label: `打款时间`, visible: true },
{ key: 11, label: `代付订单号`, visible: true },
{ key: 12, label: `审核人`, visible: true },
{ key: 13, label: `审核时间`, visible: true },
{ key: 14, label: `备注`, visible: true },
])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    agentId: null,
    billId: null,
    withdrawAmount: null,
    arriveAmount: null,
    transectionFee: null,
    withdrawRate: null,
    withdrawFee: null,
    auditStatus: null,
    auditType: null,
    arriveStatus: null,
    arriveTime: null,
    anotherPayBill: null,
    auditBy: null,
    auditTime: null,
  },
  rules: {
    agentId: [
      { required: true, message: "服务商id不能为空", trigger: "blur" }
    ],
    billId: [
      { required: true, message: "提现账单id不能为空", trigger: "blur" }
    ],
    withdrawAmount: [
      { required: true, message: "提现金额不能为空", trigger: "blur" }
    ],
    arriveAmount: [
      { required: true, message: "打款金额不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询服务商体现审核列表 */
function getList() {
  loading.value = true
  listWithdraw(queryParams.value).then(response => {
    withdrawList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    agentId: null,
    billId: null,
    withdrawAmount: null,
    arriveAmount: null,
    transectionFee: null,
    withdrawRate: null,
    withdrawFee: null,
    auditStatus: null,
    auditType: null,
    arriveStatus: null,
    arriveTime: null,
    anotherPayBill: null,
    auditBy: null,
    auditTime: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("withdrawRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加服务商体现审核"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getWithdraw(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改服务商体现审核"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["withdrawRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateWithdraw(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addWithdraw(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除服务商体现审核编号为"' + _ids + '"的数据项？').then(function() {
    return delWithdraw(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('agent/withdraw/export', {
    ...queryParams.value
  }, `withdraw_${new Date().getTime()}.xlsx`)
}

getList()

/** 动态计算字段数 **/
const fieldCount = ref(0)

onMounted(() => {
  // 统计表单字段数量
  fieldCount.value = [
    "agentId",
    "billId",
    "withdrawAmount",
    "arriveAmount",
    "transectionFee",
    "withdrawRate",
    "withdrawFee",
    "auditStatus",
    "auditType",
    "arriveStatus",
    "arriveTime",
    "anotherPayBill",
    "auditBy",
    "auditTime",
    "remark",
  ].length
})

const isTwoColumnForm = computed(() => fieldCount.value > 8)
const dialogWidth = computed(() => isTwoColumnForm.value ? '60vw' : '500px')

</script>
